﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            
            if (tbxUserName.Text == "")
            {
                labelResult.Text = "Please Enter UserName";
                return;
            }
            if (tbxPassword.Text == "")
            {
                labelResult.Text = "Please Enter Password";
                return;
            }
            DataTable dt = new DataTable();
            int iResult = SQLHelper.ExecuteLogin(tbxUserName.Text, tbxPassword.Text, out dt);
            if (iResult == -1)
            {
                labelResult.Text = "No UserName";
                return;
            }
            else if (iResult == 0)
            {
                labelResult.Text = "Wrong Password";
                return;
            }
            Entity.UserInfo m_UserInfo = new Entity.UserInfo(Convert.ToString(dt.Rows[0]["UserName"]),Convert.ToString(dt.Rows[0]["Type"]),Convert.ToInt32(dt.Rows[0]["Id"]));
            Session.Add("UserInfo", m_UserInfo);
            if (((Entity.UserInfo)Session["UserInfo"]).UserType == "admin")
                Response.Redirect("~/default.aspx");
            if (((Entity.UserInfo)Session["UserInfo"]).UserType == "School")
                Response.Redirect("~/WelcomeSchoolAccount.aspx");
            Response.Redirect("~/Search.aspx");
        }
    }
}